<?php
//Note: The session_start() function must appear BEFORE the <html> tag.
//Start the session
session_start();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP Sessions</title>
</head>
<body>
<h1>PHP Sessions</h1>

<div>
    <h3>Start a PHP Session</h3>
    <?php
    //set session variables
    $_SESSION["favcolor"] = "green";
    $_SESSION["favanimal"] = "cat";
    echo("Session variables are set.");
    ?>
</div>
<div>
    <h3>Get PHP Session Variable Values</h3>
    <?php
    echo("Favorite color is " . $_SESSION["favcolor"] . ".<br>");
    echo("Favorite animal is " . $_SESSION["favanimal"] . ".");

    //Another way to show all the session variable values for a user session is to run the following code:
    print_r($_SESSION);
    ?>
</div>
<div>
    <h3>Modify a PHP Session Variable</h3>
    <?php
    //to change a session variable, just overwrite it
    $_SESSION["favcolor"] = "yellow";
    print_r($_SESSION);
    ?>
</div>
<div>
    <h3>Destroy a PHP Session</h3>
    <?php
    //remove all session variables
    session_unset();
    var_dump($_SESSION);
    //destroy the session
    session_destroy();
    var_dump($_SESSION);
    /*
     * session_unset just clears the $_SESSION variable. It’s equivalent to doing:

$_SESSION = array();
So this does only affect the local $_SESSION variable instance but not the session data in the session storage.

In contrast to that, session_destroy destroys the session data that is stored in the session storage (e.g. the session file in the file system).

Everything else remains unchanged.
     * */
    ?>
</div>
</body>
</html>